/*
 * Created by JFormDesigner on Mon Jun 24 15:05:09 CST 2024
 */

package scct.sun.ui;

import scct.sun.dao.UserDao;
import scct.sun.model.User;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author syjyy
 */
public class FormLogin extends JFrame {
    public FormLogin() {
        initComponents();
    }

    private void btnLogin(ActionEvent e) {
//       获取文本框的输入和用户名和密码
        String name = txtName.getText();
        String pass=txtPass.getText();
        System.out.println(name+","+pass);
//         name.matches("正则表达式")返回true表示成功，返回false表示失败
        if (!name.matches("^\\w{3,10}$")){
//            swing
            JOptionPane.showMessageDialog(null,"你输入的用户名不合法，请检查后重新输入");
            return;
        }
        if (!pass.matches("^\\w{3,10}$")){
            JOptionPane.showMessageDialog(null,"你输入的密码不合法，请检查后重新输入");
            return;
        }
        UserDao userDao=new UserDao();
        User user=userDao.login(name,pass);
        if (user==null){
            JOptionPane.showMessageDialog(this,"用户名或密码错误，请检查后登录");
        }else {
            JOptionPane.showMessageDialog(this,"登录成功");
            FormMain formMain=new FormMain();
            formMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            formMain.setSize(800,600);
            formMain.setLocationRelativeTo(null);
//            formMain.set
            formMain.setVisible(true);
//            隐藏当前窗体
            this.dispose();
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - sunyujie
        label1 = new JLabel();
        txtName = new JTextField();
        label2 = new JLabel();
        txtPass = new JPasswordField();
        label3 = new JLabel();
        btnLogin = new JButton();
        btnReg = new JButton();

        //======== this ========
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u767b\u5f55");
        label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 8f));
        contentPane.add(label1);
        label1.setBounds(175, 10, 44, 27);

        //---- txtName ----
        txtName.setText("tom");
        contentPane.add(txtName);
        txtName.setBounds(115, 60, 200, txtName.getPreferredSize().height);

        //---- label2 ----
        label2.setText("\u7528\u6237\u540d");
        label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 3f));
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(55, 65), label2.getPreferredSize()));

        //---- txtPass ----
        txtPass.setText("123");
        contentPane.add(txtPass);
        txtPass.setBounds(115, 140, 200, txtPass.getPreferredSize().height);

        //---- label3 ----
        label3.setText("\u5bc6\u7801");
        label3.setFont(label3.getFont().deriveFont(label3.getFont().getSize() + 3f));
        contentPane.add(label3);
        label3.setBounds(60, 145, 35, label3.getPreferredSize().height);

        //---- btnLogin ----
        btnLogin.setText("\u767b\u5f55");
        btnLogin.addActionListener(e -> btnLogin(e));
        contentPane.add(btnLogin);
        btnLogin.setBounds(new Rectangle(new Point(85, 215), btnLogin.getPreferredSize()));

        //---- btnReg ----
        btnReg.setText("\u6ce8\u518c");
        contentPane.add(btnReg);
        btnReg.setBounds(new Rectangle(new Point(245, 215), btnReg.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 300);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - sunyujie
    private JLabel label1;
    private JTextField txtName;
    private JLabel label2;
    private JPasswordField txtPass;
    private JLabel label3;
    private JButton btnLogin;
    private JButton btnReg;

    public void getDefaultCloseOperation(int exitOnClose) {
    }
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
